package com.example.administrator.testxingpiao.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.administrator.testxingpiao.bean.User;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

/**
 * Created by Administrator on 2016/8/6.
 */
public class ORMliteHelper extends OrmLiteSqliteOpenHelper {
    /*private static final  String DATA_BASE_NAME="user.db";
    private static final  String TAG="TAG";


    *//**
     * @param context'
     *
     *            创建数据库
     *//*
    public ORMLiteHelper(Context context) {

        super(context, DATA_BASE_NAME, null, 1);
    }

    @Override  //创建表格
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            int state=   TableUtils.createTable(connectionSource, User.class);
            Log.i(TAG, "onCreate: "+(state!=-1?"创建成功":"创建失败"));
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource,User.class,true);
            onCreate(database,connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }*/

    private static final String DATA_BASE_NAME = "users.db";

    public ORMliteHelper(Context context) {
        super(context, DATA_BASE_NAME,null,1);
    }

    //创建表格
    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            int state = TableUtils.createTable(connectionSource, User.class);
            log("建表状态：" + (state != -1 ? "成功":"失败"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource,User.class,true);
            onCreate(database,connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void log(String msg){
        Log.i("TAG",msg);
    }
}

